package com.project.controller.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.entity.*;
import com.project.myBeans.ResponseBean;
import com.project.service.*;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/5/16.
 */
@Controller
@RequestMapping("/internlog")
public class InternLogController extends BaseService {
    @Autowired
    private InternlogService internlogService;

    @Autowired
    private InternInfoService internInfoService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private StudentService studentService;


    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**    Todo 时间戳解析异常 转换成了Date对象
     * author: WheelChen
     * date: 2017-5-16
     * function: 模糊搜索分页显示
     * @param internLog
     * @return
     */
    public ResponseBean findByCondition(@RequestBody InternLog internLog) {
        ResponseBean responseBean = new ResponseBean();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            internLog.setPageIndex(internLog.getPageIndex() - 1);
            List<InternLog> internInfoList = internlogService.findByCondition(internLog);
            for (InternLog internLog1 : internInfoList) {
                JSONObject jsonObject = internlogService.findDetailOneByInternLog(internLog1);
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(internLog.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查询成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017/6/22
     * function: 添加实习日志
     *
     * @param internLog
     * @return
     */
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean add(@RequestBody InternLog internLog) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //1. 先找到当前学生表的id
            StudentInfo studentInfo = studentInfoService.findOneByUserId(internLog.getComposerid());
            if (studentInfo != null) {
                int studentInfoId = studentInfo.getId();
                //查找正在实习的实习信息表
                InternInfo internInfo = new InternInfo();
                internInfo.setSno(studentInfoId);
                internInfo.setOrgattr(2); //正在实习单位
                internInfo = internInfoService.findOneBySnoAndOragattr(internInfo);
                if (internInfo != null) {
                    internLog.setEmployer(internInfo.getId());
                    internlogService.add(internLog);
                    responseBean.setMessage("实习日志添加成功");
                    responseBean.setSuccess(true);
                } else {
                    responseBean.setSuccess(false);
                    responseBean.setMessage("学生无正在实习单位");
                }
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("学生信息表记录不存在");
            }
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/22
     * function: 根据id删除实习日志
     * @param id
     * @return
     */
    public ResponseBean delete(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (internlogService.delete(id) > 0)
                responseBean.setSuccess(true);
            else
                responseBean.setSuccess(false);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/22
     * function: 修改实习日志
     * @param internLog
     * @return
     */
    public ResponseBean modify(@RequestBody InternLog internLog) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (internlogService.update(internLog) > 0)
                responseBean.setSuccess(true);
            else
                responseBean.setSuccess(false);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/22
     * function: 根据id查找实习日志信息
     * @param id
     * @return
     */
    public ResponseBean findById(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            InternLog internLog = internlogService.findById(id);
            if (internLog != null) {
                JSONObject jsonObject = internlogService.findDetailOneByInternLog(internLog);
                responseBean.setData(jsonObject);
                responseBean.setSuccess(true);
            } else {
                responseBean.setMessage("实习日志记录不存在");
                responseBean.setSuccess(false);
            }
        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
        }
        return responseBean;
    }

    @RequestMapping(value = "/findDetailBatchByInternAdvisorCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/5
     * function: 根据internAdvisor查找实习日志详情（即查看该老师指导的学生的实习日志） 根据实习日志条件查找
     *
     * @param jsonObject 发送格式为 internLog：{},internAdvisorUserId:""
     * @return
     */
    public ResponseBean findDetailBatchByInternAdvisorCondition(@RequestBody JSONObject jsonObject) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //解析JSON对象 获得日志/实习指导老师userId/分页大小和页码
            InternLog internLog = new InternLog();
            Integer internAdvisorUserId = -1;
            if (jsonObject.getString("internLog") != null)//实习日志条件
                internLog = JSONObject.parseObject(jsonObject.getString("internLog"), InternLog.class);
            if (jsonObject.getString("internAdvisorUserId") != null)//实习指导老师userId
                internAdvisorUserId = Integer.parseInt(jsonObject.getString("internAdvisorUserId"));
            int pageSize = Integer.parseInt(jsonObject.getString("pageSize"));//分页大小
            int pageIndex = Integer.parseInt(jsonObject.getString("pageIndex")) - 1;//页码

            JSONObject jsonObject1 = internlogService.findDetailBatchByInternAdvisorCondition(internLog, internAdvisorUserId, pageSize,pageIndex);
            responseBean.setData(jsonObject1.get("data"));
            responseBean.setTotal((Integer) jsonObject1.get("total"));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
